<?php

/**
 * Entidad usuario.
 * */
class User {
	const DB_TUSER = 'user';
	const DB_ID = 'iduser';
	const DB_EMAIL = 'email';
	const DB_PASSWORD = 'password';
	const DB_LASTUPDATE = 'lastupdate';
	const DB_HUMANNAME = 'humanname';
	
	CONST DB_TJGROUP = "user_in_group";
	CONST DB_TJGROUP_IDUSER = "iduser";
	CONST DB_TJGROUP_GO = "go";
	CONST DB_TJGROUP_GO_ACEPT = "acept";
	CONST DB_TJGROUP_GO_REJECT = "reject";
	CONST DB_TJGROUP_GO_INVITE = "invite";
	CONST DB_TJGROUP_CREATOR = "creator";
	
	CONST DB_TPGROUP = "place_in_group";
	CONST DB_TPGROUP_IDPLACE = "idplace";	
	
	const DB_TFRIEND = 'friend';
	const DB_TF_IDUSER = 'iduser';
	const DB_TF_IDFRIEND = 'friend';
	const DB_TF_ACTIVE = 'active';
	
	const DB_TUGPLACE = "user_in_group_in_place";
	const DB_TUGPLACE_GO = "go";
	const DB_TUGPLACE_GO_GO = "go";
	const DB_TUGPLACE_NOTGO = "notgo";
	
	/**
	 * Identificador del usuario.
	 * @var int
	 * */
	private $id;
	
	/**
	 * Email del usuario.
	 * @var string
	 * */
	private $email;
	
	/**
	 * Contraseña del usuario.
	 * @var string
	 * */
	private $password;
	
	private $humanName;
	private $lastupdate;
	
	private $signature;
	
	private $others;
	
	/**
	 * Devuelve el identificador del usuario.
	 * @return int Será -1 si el usuario no está correctamente logueado.
	 * */
	function getId(){
		return $this->id;
	}
	
	/**
	 * Devuelve el email del usuario.
	 * @return string Email del usuario.
	 * */
	function getEmail(){
		return $this->email;
	}
	
	/**
	 * Devuelve la contraseña del usuario.
	 * @return string La contraseña del usuario.
	 * */
	function getPassword(){
		return $this->password;
	}
	
	/**
	 * Devuelve la firma del usuario logueado.
	 * @return string Firma del usuario.
	 * */
	function getSignature(){
		return $this->signature;
	}
	
	/**
	 * Devuelve la lista de amigos del usuario.
	 * @return array Lista de amigos.
	 * */
	function getFriendList(){
		return $this->friends;
	}
	
	function putId($id){
		$this->id = $id;
	}
	
	
	function putEmail($email){
		$this->email= $email;
	}
	

	function putPassword($password){
		$this->password = $password;
	}
	
	function putHumanName($humanname){
		$this->humanName= $humanname;
	}
	
	
	function putSignature($signature){
		$this->signature = $signature;
	}
	
	function putLastUpdate($lastupdate){
		$this->lastupdate = $lastupdate;
	}
	
	function putFriendList($friendList){
		$this->friends = $friendList;
	}
	
	function putOther($key, $value){
		$this->others[$key] = $value;
	}
	
	public function __construct(){
		$this->others = array();
	}
	
	/**
	 * Comprueba si existe el usuario o no. Sino existe comprueba que sea un usuaro de Destinia.com y acto seguido lo loguea como nuestro.
	 * @param string $mail Email del usuario a loguear.
	 * @param string $password Password del usuario a loguear.
	 * @return bool Si está correctamente logueado será TRUE sino devlverá FALSE.
	 * */
	public function login($email, $password){
		$this->email = $email;
		$this->password = $password;
		$emailform = formsql($this->email);
		$pass = md5($this->password);
		$q = "SELECT * FROM ".self::DB_TUSER." WHERE ".self::DB_EMAIL." LIKE '".$emailform."' AND ".self::DB_PASSWORD." LIKE '".$pass."'";
		$res = sendsql($q);
		
		if(!empty($res)){
			$this->id = $res[0][self::DB_ID];
			$this->humanName = $res[0][self::DB_HUMANNAME];
			$this->lastupdate = $res[0][self::DB_LASTUPDATE];
			
			return true;
		} /*elseif($this->loginDestinia()){
			$q = "INSERT INTO ".self::DB_TUSER." (".self::DB_EMAIL.", ".self::DB_PASSWORD.") VALUES ('".$emailform."', '".$pass."')";
			sendsql($q);
			
			return true;
		} */else {
			
			return false;
		}
	}
	
		/**
	 * Registra un usuario.
	 * @param string $mail Email del usuario a registrar.
	 * @param string $password Password del usuario a registrar.
	 * @return bool Si lo ha registrado será TRUE sino devlverá FALSE.
	 * */
	public function register($email, $password, $humanname){
		$this->email = $email;
		$this->password = $password;
		$this->humanName = formsql(to_utf8($humanname));
		$emailform = formsql($this->email);
		$pass = md5($this->password);
		$q = "SELECT * FROM ".self::DB_TUSER." WHERE ".self::DB_EMAIL." LIKE '".$emailform."' AND ".self::DB_PASSWORD." LIKE '".$pass."'";
		$res = sendsql($q);
		
		if(empty($res)){
			$this->lastupdate = time();
			$q = "INSERT INTO ".self::DB_TUSER." (".self::DB_EMAIL.", ".self::DB_PASSWORD.", ".self::DB_HUMANNAME.", ".self::DB_LASTUPDATE.") VALUES ('".$emailform."', '".$pass."','".$this->humanName."',".$this->lastupdate.")";
			$res = sendsql($q, true);
			$this->id = $res['id'];
			return true;			
		} else {
			return false;			
		}
		
	}
	
	/**
	 * Carga un usuario de la base de datos.
	 * @param integer $iduser Identificador del usuario.
	 * */
	public function load($idUser){
		$q = "SELECT * FROM `".self::DB_TUSER."` WHERE ".self::DB_ID." = ".$idUser;
		$res = sendsql($q);
		
		if(!empty($res)){
			$this->id = $idUser;
			$this->email = $res[0][self::DB_EMAIL];
			$this->password = $res[0][self::DB_PASSWORD];
			$this->humanName = $res[0][self::DB_HUMANNAME];
			$this->lastupdate = $res[0][self::DB_LASTUPDATE];
			return true;
		} else {
			return false;			
		}
	}
	
	/**
	 * Obtiene un usuario basándose en su email.
	 * @param string $email
	 * */
	public function getUserByMail($email){
		$email = formsql($email);
		$q = "SELECT * FROM ".self::DB_TUSER." WHERE ".self::DB_EMAIL." LIKE '".$email."' ";
		$res = sendsql($q);
		
		if(!empty($res)){
			$this->email = $res[0][self::DB_EMAIL];
			$this->password = $res[0][self::DB_PASSWORD];
			$this->id = $res[0][self::DB_ID];
			$this->humanName = $res[0][self::DB_HUMANNAME];
			$this->lastupdate = $res[0][self::DB_LASTUPDATE];
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Comprueba si el usuario está logueado en destinia.com y si existe lo añade a la base de datos local.
	 * 
	 * @return bool Si existe en destinia.
	 * */
	private function loginDestinia(){
		$result = Troovel::login($this->email, $this->password);
		$this->signature = $result;
		if(is_null($result)){
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Obtiene todos los amigos del usuario.
	 * */
	public function listFriends(){
		$q = "SELECT u.*, f.acepted FROM ".self::DB_TUSER." u LEFT JOIN `".self::DB_TFRIEND."` f ON u.".self::DB_ID." = f.".self::DB_TF_IDFRIEND." WHERE f.".self::DB_TF_IDUSER." = ".$this->id." AND u.".self::DB_TF_ACTIVE." = 1 AND f.".self::DB_TF_ACTIVE." = 1";
		$res = sendsql($q);
		
		$this->friends = $res;
		
		$result = array();
		for($i = 0; $i < count($this->friends); $i++){
			$friend = new User();
			$friend->putEmail($this->friends[$i][self::DB_EMAIL]);
			$friend->putId($this->friends[$i][self::DB_ID]);
			if($this->isFriendOf($friend->getId())) $friend->putHumanName($this->friends[$i][self::DB_HUMANNAME]);
			else $friend->putHumanName('');
			$friend->putLastUpdate($this->friends[$i][self::DB_LASTUPDATE]);
			
			$result[] = $friend->tojson();
			
			unset($friend);		
		}
		
		return $result;
	}
	
	/**
	 * Comprueba si un amigo nos tiene en su lista de amigos.
	 * */
	public function isFriendOf($idFriend){
		$q = "SELECT 1 FROM ".self::DB_TFRIEND." WHERE ".self::DB_ID." = ".$idFriend." AND ".self::DB_TF_IDFRIEND." = ".$this->id." AND ".self::DB_TF_ACTIVE." = 1";
		$res = sendsql($q);
		
		if(empty($res)){
			return false;
		} else {
			return true;			
		}		
	}
	
	/**
	 * Añade un nuevo amigo.
	 * @param User $newFriend Usuario amigo.
	 * */
	public function addFriend($newFriend){
		$q = "SELECT ".self::DB_TF_ACTIVE." FROM ".self::DB_TFRIEND." WHERE ".self::DB_TF_IDUSER." = ".$this->id. " AND ".self::DB_TF_IDFRIEND." = ".$newFriend->getId();
		$res = sendsql($q);
		
		if(empty($res)){
			$q = "INSERT INTO `".self::DB_TFRIEND."` (".self::DB_TF_IDUSER.", ".self::DB_TF_IDFRIEND.") VALUES (".$this->id.", ".$newFriend->getId().")";
			sendsql($q, true);
		} else {
			if(!$res[self::DB_TF_ACTIVE]) {
				$q = "UPDATE `".self::DB_TFRIEND."` SET `".self::DB_TF_ACTIVE."` = 1 WHERE ".self::DB_TF_IDUSER." = ".$this->id." AND ".self::DB_TF_IDFRIEND." = ".$newFriend->getId();
				sendsql($q);			
			}
			
		}
	}
	
	/**
	 * Borra un amigo.
	 * @param integer $friendId El identificador de usuario del amigo.
	 * */
	public function deleteFriend($friendId){
		$q = "UPDATE `".self::DB_TFRIEND."` SET `".self::DB_TF_ACTIVE."` = 0 WHERE ".self::DB_TF_IDUSER." = ".$this->id." AND ".self::DB_TF_IDFRIEND." = ".$friendId;
		$res = sendsql($q);
	}
	
	/**
	 * Comprueba si el usuario tiene una relación de amistadcon idFriend.
	 * @param integer $friendId El identificador de usuario del amigo.
	 * @return boolean True, si tienen una relación de amistad. False, en caso contrario.
	 * */
	public function hasFriend($idFriend){
		$q = "SELECT 1 FROM `".self::DB_TFRIEND."` WHERE ".self::DB_TF_IDUSER." = ".$this->id. " AND ".self::DB_TF_IDFRIEND." = ".$idFriend;
		$res = sendsql($q);
		
		if(empty($res)) return FALSE;
		else return TRUE;
	}
	
	/**
	 * Crea un nuevo grupo.
	 * @param string $groupName Nombre del grupo.
	 * @return Group El grupo creado.
	 * */
	public function createGroup($groupName){
		$newGroup = new Group();
		$newGroup->create($groupName, $this);
		$this->addFriend2Group($this->id, $newGroup->getId(), self::DB_TJGROUP_GO_ACEPT);
		return $newGroup;
	}
	
	/**
	 * Elimina un grupo.
	 * @param $group Grupo a eliminar
	 * @return True en caso de que haya borrado el grupo, false en caso contrario.
	 * */
	public function deleteGroup(&$group){
		if($group->getOwner()->getId() == $this->getId()){ // Exista el grupo y el usuario sea creador.
			$group->deleteFromDB();
		}
		
	}
	
	/**
	 * Comprueba si el usuario es el creador del grupo.
	 * @param integer $idGrupo Identificador del grupo
	 * @return boolean True, si el usuario es creador del grupo. False, en caso contrario.
	 * */
	public function manageGroup($idGroup){
		$q = "SELECT 1 FROM `".Group::DB_TGROUP."` WHERE ".Group::DB_TGROUP_ID." = ".$idGroup. " AND ".Group::DB_TGROUP_CREATOR." = ".$this->id;
		$res = sendsql($q);
		
		if(empty($res)) return FALSE;
		else return TRUE;
		
	}
	
	/**
	 * Carga los grupos donde participa un usuario.
	 * @return lista de grupos
	 * */
	public function listGroups(){
		$q = "SELECT * FROM `".Group::DB_TGROUP."` WHERE ".Group::DB_TGROUP_ID." in (SELECT ".Group::DB_TGROUP_ID." FROM `".self::DB_TJGROUP."` WHERE ".self::DB_TJGROUP_IDUSER." = ".$this->id.")";
		$res = sendsql($q);
		
		$result = array();
		if(!empty($res)){
			for($i = 0; $i < count($res); $i++){
				$group = new Group();
				$group->putId($res[$i]['idgroup']);	
				$group->putName($res[$i]['name']);
				$group->putLastUpdate($res[$i]['lastupdate']);
				$owner = new User();
				$owner->load($res[$i]['creator']);
				$group->putOwner($owner);
				$group->loadInvitedUsers($group->getId());
				$group->loadComments();
				$group->loadPlaces();
				$result[$i] = $group->tojson();
				
				unset($owner);
				unset($group);
			}			
		}
		
		return $result;
		
	}
	
	/**
	 * Añade un lugar en un grupo.
	 * @param integer $idPlace Lugar a añadir.
	 * @param integer $idGrup Grupo en el que añadir el lugar.
	 * */
	public function addPlace2Group($idPlace, $idGrup){
		$q = "SELECT 1 FROM `".self::DB_TPGROUP."` WHERE ".Group::DB_TGROUP_ID." = ".$idGrup." AND ".self::DB_TPGROUP_IDPLACE." = ".$idPlace;
		$res = sendsql($q);
		
		if(empty($res)){
			$q = "INSERT INTO ".self::DB_TPGROUP." (".Group::DB_TGROUP_ID.",".self::DB_TPGROUP_IDPLACE.") VALUES(".$idGrup.",".$idPlace.")";
			sendsql($q);
		} else if($res[0][self::DB_TF_ACTIVE] == 0) {
			$q = "UPDATE ".self::DB_TPGROUP." SET ".self::DB_TF_ACTIVE." = 1 WHERE ".Group::DB_TGROUP_ID." = ".$idGrup." AND ".self::DB_TPGROUP_IDPLACE." = ".$idPlace;
			sendsql($q);			
		}
	}
	
	/**
	 * Elimina un lugar de un grupo.
	 * @param integer $idPlace Lugar a eliminar.
	 * @param integer $idGrup Grupo a eliminar.
	 * */
	public function deletePlace4Group($idPlace, $idGrup){
		$q = "UPDATE ".self::DB_TPGROUP." SET ".self::DB_TF_ACTIVE." = 0 WHERE ".Group::DB_TGROUP_ID." = ".$idGrup." AND ".self::DB_TPGROUP_IDPLACE." = ".$idPlace;
		$res = sendsql($q);		
	}
	
	/**
	 * Añade un amigo al grupo.
	 * @param integer $idFriend Identificador del amigo.
	 * @param integer $idGroup Identificador del grupo.
	 * @param string $go Estado de ida con el grupo (aceptado, rechazado, invitado).
	 * */
	public function addFriend2Group($idFriend, $idGroup, $go='invite'){
		$q = "SELECT 1 FROM `".self::DB_TJGROUP."` WHERE ".self::DB_TJGROUP_IDUSER." = ".$idFriend." AND ".Group::DB_TGROUP_ID." = ".$idGroup;
		$res = sendsql($q);
		
		if(empty($res)){
			$q = "INSERT INTO `".self::DB_TJGROUP."` (".Group::DB_TGROUP_ID.",".self::DB_TJGROUP_IDUSER.",".self::DB_TJGROUP_GO.") VALUES(".$idGroup.",".$idFriend.",'".$go."')";
			sendsql($q);
		} else if($res[0][self::DB_TF_ACTIVE] == 0) {
			$q = "UPDATE ".self::DB_TJGROUP." SET ".self::DB_TF_ACTIVE." = 1 WHERE ".self::DB_TJGROUP_IDUSER." = ".$idFriend." AND ".Group::DB_TGROUP_ID." = ".$idGrup;
			sendsql($q);			
		}
	}
	
	/**
	 * Borra un amigo del grupo
	 * @param integer $idFriend Identificador del amigo.
	 * @param integer $idGroup Identificador del grupo.
	 * */
	public function delFriend4Group($idFriend, $idGroup){
		$q = "UPDATE ".self::DB_TJGROUP." SET ".self::DB_TF_ACTIVE." = 0, ".self::DB_TJGROUP_GO." = '".self::DB_TJGROUP_GO_INVITE."'  WHERE ".self::DB_TJGROUP_IDUSER." = ".$idFriend." AND ".Group::DB_TGROUP_ID." = ".$idGroup;
		$res = sendsql($q);			
	}
	
	/**
	 * Acepta o rechaza la invitación a un grupo
	 * @param integer $idGroup Identificador del grupo
	 * @param string $state Aceptar o rechazar
	 * */
	public function putStateInvitation4Group($idGroup, $state = 'acept'){
		$q = "UPDATE ".self::DB_TJGROUP." SET ".self::DB_TF_ACTIVE." = 1, ".self::DB_TJGROUP_GO." = '".$state."' WHERE ".self::DB_TJGROUP_IDUSER." = ".$this->id." AND ".Group::DB_TGROUP_ID." = ".$idGroup;
		$res = sendsql($q);
	}
	
	/**
	 * Añade un comentario.
	 * */
	public function addComment($idGroup, $idPlace, $msg){
		$comment = new Comment($idGroup, $this->id, $msg, $idPlace);
		$comment->saveToDB();
		
		return $comment;		
	}
	
	public function isPlaceInGroup($idPlace, $idGroup){
		$q = "SELECT 1 FROM `".self::DB_TPGROUP."` WHERE ".Group::DB_TGROUP_ID." = ".$idGroup." AND ".self::DB_TPGROUP_IDPLACE." = ".$idPlace;
		$res = sendsql($q);
		
		if(empty($res)) return false;
		else return true;
	}
	
	public function goPlace4Group($idPlace, $idGroup, $go){
		switch($go){
			case 0:
				$go = self::DB_TUGPLACE_NOTGO;
				break;
			case 1:
				$go = self::DB_TUGPLACE_GO;
				break;
		}
		$q = "SELECT 1 FROM `".self::DB_TUGPLACE."` WHERE ".Place::DB_TPLACE_ID." = ".$idPlace." AND ".Group::DB_COMMENT_IDGROUP." = ".$idGroup;
		$res = sendsql($q);
		
		if(empty($res)){
			$q = "INSERT INTO `".self::DB_TUGPLACE."` (".self::DB_ID.",".Group::DB_COMMENT_IDGROUP.", ".Place::DB_TPLACE_ID.", ".self::DB_TUGPLACE_GO.") VALUES(".$this->id.",".$idGroup.",".$idPlace.",'".$go."')";
			sendsql($q);
		} else {
			$q = "UPDATE `".self::DB_TUGPLACE."` SET ".self::DB_TF_ACTIVE." = 1, ".self::DB_TUGPLACE_GO." = '".$go."' WHERE ".Place::DB_TPLACE_ID." = ".$idPlace." AND ".Group::DB_COMMENT_IDGROUP." = ".$idGroup;
			sendsql($q);
		}		
	}
	
	public function isInvitedInGroup($idGroup){
		$q = "SELECT 1 FROM `".self::DB_TJGROUP."` WHERE ".self::DB_TJGROUP_IDUSER." = ".$this->id." AND ".Group::DB_TGROUP_ID." = ".$idGroup;
		$res = sendsql($q);
		
		if(empty($res)) return false;
		else return true;
	}
	
	public function getUserFromDB($idUser){
		$q = "SELECT * 
FROM `user` u
WHERE
	u.iduser = ".$idUser." AND u.active = 1
	AND (
		EXISTS(SELECT 1 FROM `friend` f WHERE f.iduser = u.iduser AND f.friend = ".$this->id." AND f.active = 1 AND EXISTS(SELECT 1 FROM `friend` f2 WHERE f2.iduser = f.friend AND f.friend = u.iduser AND f2.active = 1))
		OR
		EXISTS(SELECT 1 FROM `friend` f WHERE f.active = 1 AND f.iduser = ".$this->id." AND f.friend IN (SELECT f2.friend FROM `friend` f2 WHERE f2.iduser = f.iduser AND f2.active = 1 AND EXISTS(SELECT 1 FROM `friend` f3 WHERE f3.iduser = f2.friend AND f3.friend = f2.iduser AND f3.active = 1)) AND EXISTS(SELECT 1 FROM `friend` f2 WHERE f2.iduser = f.friend AND f2.friend = u.iduser AND f2.active = 1 AND EXISTS(SELECT 1 FROM `friend` f3 WHERE f3.iduser = u.iduser AND f3.friend = f.friend AND f3.active = 1)) )
	)";
		$res = sendsql($q);
		
		$user = null;
		if(!empty($res)){
			$user = new User();
			$user->putId($idUser);
			$user->putEmail($res[0][self::DB_EMAIL]);
			$user->putPassword($res[0][self::DB_PASSWORD]);
			$user->putHumanName($res[0][self::DB_HUMANNAME]);
			$user->putLastUpdate($res[0][self::DB_LASTUPDATE]);
		}
		return $user;
		
	}
	
	/**
	 * Añade una nueva entrada a la bd-
	 * */
	public function addNewEntry($idGroup, $idPlace, $text, $feeling){
		$entry = new Entry();
		$entry->putIdGroup($idGroup);
		$entry->putIdPlace($idPlace);
		$entry->putAutor($this->id);
		$entry->putText($text);
		$entry->putFeeling($feeling);
		
		$entry->saveToDB();
		
		return $entry;		
	}
	
	public function deleteEntry($idEntry){
		$q = "UPDATE ".Entry::DB_TENTRY." SET active = 0 WHERE ".Entry::DB_TENTRY_ID." = ".$idEntry;
		sendsql($q);		
	}
	
	/**
	 * Devuelve todas las entradas de interes para el usuario.
	 * */
	public function getEntries(){
		$q = "SELECT * FROM `".Entry::DB_TENTRY."` e WHERE (e.".Entry::DB_TENTRY_AUTOR." IN (SELECT f.".self::DB_TFRIEND." from `".self::DB_TFRIEND."` f WHERE f.".self::DB_TF_ACTIVE." = 1 AND f.".self::DB_TF_IDUSER." = ".$this->id." AND EXISTS(SELECT 1 FROM `".self::DB_TFRIEND."` f2 WHERE f2.".self::DB_TF_ACTIVE." = 1 AND f2.".self::DB_TF_IDUSER." = f.".self::DB_TFRIEND." AND f2.".self::DB_TFRIEND." = f.".self::DB_TF_IDUSER.")) OR 
e.".Entry::DB_TENTRY_AUTOR." IN (SELECT ug.".self::DB_TJGROUP_IDUSER." FROM `".self::DB_TJGROUP."` ug WHERE ug.".Group::DB_TGROUP_ID." IN (SELECT ug2.".Group::DB_TGROUP_ID." FROM `".self::DB_TJGROUP."` ug2 WHERE ug2.".self::DB_TJGROUP_IDUSER." = ".$this->id." AND ug2.".self::DB_TJGROUP_GO." like '".self::DB_TJGROUP_GO_ACEPT."' AND ug2.".self::DB_TF_ACTIVE." = 1) AND ug.".self::DB_TJGROUP_GO." like '".self::DB_TJGROUP_GO_ACEPT."' AND ug.".self::DB_TF_ACTIVE." = 1)) and e.".self::DB_TF_ACTIVE." = 1";
		$res = sendsql($q);
		
		$entries = array();
		
		for($i = 0; $i < count($res); $i++){
			$idGroup= $res[$i][Group::DB_TGROUP_ID];
			$idPlace = $res[$i][self::DB_TPGROUP_IDPLACE];
			$autor = $res[$i][Entry::DB_TENTRY_AUTOR];
			$text = $res[$i][Entry::DB_TENTRY_TEXT];
			$feeling = $res[$i][Entry::DB_TENTRY_FEELING];
			$lupdate = $text = $res[$i][Entry::DB_TENTRY_LUPDATE];
			
			$entry = new Entry($idGroup, $idPlace, $autor, $text, $feeling, $images);
			$entry->putId($res[$i][Entry::DB_TENTRY_ID]);
			$entry->putLastUpdate($lupdate);
			$entry->loadImages();
				
			$entries[] = $entry;			
		}
		
		return $entries;		
	}
	
	public function tojson(){
		$result = array(
			idUser => $this->id,
			email => $this->email,
			userName => $this->humanName,
			lastUpdate => $this->lastupdate
		);
		foreach ($this->others as $key => $value) {
			$result[$key] = $value;
		}
		return $result;
	}
	
}

?>